package com.dadibadi.core.work.controller;


import com.dadibadi.core.dao.entity.WorkInfoTab;
import com.dadibadi.core.dao.mapper.WorkInfoMapper;
import com.dadibadi.core.user.qo.UserInfoEditQO;
import com.dadibadi.core.user.service.UserInfoService;
import com.dadibadi.core.work.qo.WorkInfoEditQO;
import com.venusoft.developer.common.result.CommonResult;
import com.venusoft.developer.common.result.ICommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * TODO
 *
 * @author : lkfs@163.com
 * @date : 2025-08-18 17:00
 * 保存作品，若id为空则为新作品，id不为空则更新
 */

@RestController
@RequestMapping("/work")
@Slf4j
@Tag(name = "api/作品信息", description = "")
public class ApiWorkInfoSaveController {
    @Resource
    private WorkInfoMapper workInfoMapper;
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/api-work-info-save")
    @Operation(summary = "保存作品", description = "")
    public ICommonResult exec(
            @RequestBody @Parameter(required = true) @Validated WorkInfoEditQO qo,
            @RequestHeader(required = false) String platform,
            @RequestHeader(required = false) String version,
            @RequestHeader(required = false, name = "Accept-Language") String language,
            HttpServletRequest request) {

        WorkInfoTab workInfoTab;
        if (ObjectUtils.isEmpty(qo.getWorkId())) {
            workInfoTab = new WorkInfoTab();
            BeanUtils.copyProperties(qo, workInfoTab);
            workInfoMapper.insert(workInfoTab);
        } else {
            workInfoTab = workInfoMapper.selectById(qo.getWorkId());
            BeanUtils.copyProperties(qo, workInfoTab);
            workInfoMapper.updateById(workInfoTab);
        }
        UserInfoEditQO userInfoEditQO= UserInfoEditQO.builder().userId(qo.getUserId()).build();
        userInfoService.updateArticleCount(userInfoEditQO);
        return new CommonResult<>(workInfoTab.getWorkId());
    }
}
